
Partial Class DashboardControls_Notifications
    Inherits System.Web.UI.UserControl
    Implements IDashBoard

    Dim titleLabelString As String = ""
    Dim pageSizeValue As Integer = 0

    Public Property SortDirection() As System.Web.UI.WebControls.SortDirection Implements IDashBoard.SortDirection
        Get
            If ViewState("SortDirection") Is Nothing Then
                Return WebControls.SortDirection.Ascending
            Else
                Return ViewState("SortDirection")
            End If
        End Get
        Set(ByVal value As System.Web.UI.WebControls.SortDirection)
            ViewState("SortDirection") = value
        End Set
    End Property

    Public Property PageNumber() As Integer Implements IDashBoard.PageNumber
        Get
            Return Me.NotificationsGridView.PageIndex
        End Get
        Set(ByVal value As Integer)
            Me.NotificationsGridView.PageIndex = value
        End Set
    End Property

    Public Property PageSize() As Integer Implements IDashBoard.PageSize
        Get
            If Not Me.NotificationsGridView Is Nothing Then
                If NotificationsGridView.AllowPaging = False Then
                    Return -1
                Else
                    Return Me.NotificationsGridView.PageSize
                End If

            Else
                Return pageSizeValue
            End If
        End Get
        Set(ByVal value As Integer)
            If Not Me.NotificationsGridView Is Nothing Then
                If value = -1 Then
                    NotificationsGridView.AllowPaging = False
                Else
                    NotificationsGridView.AllowPaging = True
                    Me.NotificationsGridView.PageSize = value
                End If

                Me.NumberOfRowsDropDownList.SelectedValue = value
            End If
            pageSizeValue = value
        End Set
    End Property

    Public Property SortField() As String Implements IDashBoard.SortField
        Get
            If ViewState("SortField") Is Nothing Then
                Return ""
            Else
                Return ViewState("SortField")
            End If
        End Get
        Set(ByVal value As String)
            ViewState("SortField") = value
        End Set
    End Property

    Public Property Title() As String Implements IDashBoard.Title
        Get
            If Me.lblTitle IsNot Nothing Then
                Return lblTitle.Text
            Else
                Return titleLabelString
            End If
        End Get
        Set(ByVal value As String)
            If lblTitle IsNot Nothing Then
                lblTitle.Text = value
            End If
            titleLabelString = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        AddHandler NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        If Not String.IsNullOrEmpty(titleLabelString) Then
            Me.lblTitle.Text = titleLabelString
        End If
        If pageSizeValue > 0 Then
            Me.PageSize = pageSizeValue
        End If


        If Not Me.IsPostBack Then
            GridViewSettingController.SetGridViewValues(Me, 5, "DateGenerated", WebControls.SortDirection.Descending)
            Me.BindGrid()
        End If
    End Sub

    Protected Sub NumberOfRowsDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumberOfRowsDropDownList.SelectedIndexChanged

        If NumberOfRowsDropDownList.SelectedValue = "-1" Then
            Me.NotificationsGridView.AllowPaging = False
        Else
            NotificationsGridView.AllowPaging = True
            NotificationsGridView.PageSize = NumberOfRowsDropDownList.SelectedValue
            NotificationsGridView.PageIndex = 0
        End If
        BindGrid()
    End Sub

    Public Sub BindGrid() Implements IDashBoard.BindGrid

        Me.NotificationsGridView.DataSource = Cdm.DataCore.CaseNotes.UserNotificationDisplay.ListByUser(Session("CurrentUserId"), Me.SortField, Me.SortDirection.Equals(SortDirection.Ascending))
        Me.NotificationsGridView.PageSize = Me.NumberOfRowsDropDownList.SelectedValue
        Me.NotificationsGridView.DataBind()
        GridViewSettingController.SaveSetting(Me)
    End Sub

    Protected Sub NotificationsGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotificationsGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(Me.NotificationsGridView)
    End Sub

    Protected Sub grvNotifications_PageIndexChanging(ByVal sender As Object, _
                                                     ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) _
                                                     Handles NotificationsGridView.PageIndexChanging

        Me.PageNumber = e.NewPageIndex
        BindGrid()

    End Sub

    Public Sub LoadControl1(ByVal sender As Object, ByVal e As System.EventArgs) Implements IDashBoard.LoadControl
        Me.Page_Load(sender, e)
    End Sub

    Protected Sub NotificationsGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles NotificationsGridView.RowDataBound
        Dim removeButton As Button = e.Row.FindControl("RemoveButton")
        If Not removeButton Is Nothing Then

            Dim deleteNotification As Cdm.DataCore.CaseNotes.UserNotificationDisplay = e.Row.DataItem
            Dim BLANK_MESSAGE As String
            Dim msg As String
            BLANK_MESSAGE = "Are you sure you want to remove the {0} notification for {1} entitled '{2}'?"
            msg = String.Format(BLANK_MESSAGE, deleteNotification.DateGenerated.ToString(), _
            deleteNotification.FullName, deleteNotification.NotificationSubject)
            removeButton.Attributes.Add("onclick", String.Format("return confirm('{0}');", msg))
        End If
    End Sub

    Protected Sub NotificationsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles NotificationsGridView.Sorting
        If e.SortExpression.Equals(Me.SortField) And Me.SortDirection.Equals(SortDirection.Ascending) Then
            Me.SortDirection = WebControls.SortDirection.Descending
        Else
            Me.SortDirection = WebControls.SortDirection.Ascending
        End If
        Me.SortField = e.SortExpression
        BindGrid()
    End Sub

    Protected Sub Remove_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim removeButton As Button = CType(sender, Button)
        Dim id As Integer = CInt(removeButton.CommandArgument)
        Dim deleteNotification As Cdm.DataCore.UserSecurity.UserNotification
        deleteNotification = Cdm.DataCore.UserSecurity.UserNotification.Retrieve(id)
        deleteNotification.IsActive = False
        deleteNotification.Persist()
        BindGrid()
    End Sub
End Class
